import json
from multiprocessing import context
from xml.etree.ElementTree import canonicalize
from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from rubbish.models import Food, Hazardous, Recyclable, Residual
# Create your views here.
from django.core.mail import send_mail
def home(request):
    context={}
    context['Recyclable']=(Recyclable.objects.latest("created"))
    context['Residual']=(Residual.objects.latest("created"))
    context['Hazardous']=(Hazardous.objects.latest("created"))
    context['Food']=(Food.objects.latest("created"))
    # context={'info',caninfo}
    return render(request, 'list.html',context)

@csrf_exempt

def upload(request):
    if request.method=='GET':
        print(request.GET["msg"])
        msg=request.GET["msg"]
        return HttpResponse(msg)
    elif (request.method == 'POST'):
        print("the POST method")
        concat = request.POST
        postBody = request.body
        json_result = json.loads(postBody)
        data=float(json_result['msg']["value"])
        if(json_result['msg']["ds_id"]=="Recyclable"):
            tmp=Recyclable(data=data/1.1)
            tmp.save()
            print(tmp)
        elif json_result['msg']["ds_id"]=="Residual":
            tmp=Residual(data=data/1.05)
            tmp.save()
            print(tmp)
        elif json_result['msg']["ds_id"]=="Hazardous":
            tmp=Hazardous(data=data/1.18)
            tmp.save()
            print(tmp)
        elif json_result['msg']["ds_id"]=="Food":
            tmp=Food(data=data/1.12)
            tmp.save()
            print(tmp)
        print(json_result)
        return HttpResponse("400")
